package com.huaju.enity;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import static org.hibernate.criterion.Example.create;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 * A data access object (DAO) providing persistence and search support for
 * Bankcard entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.huaju.enity.Bankcard
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class BankcardDAO {
	private static final Logger log = LoggerFactory
			.getLogger(BankcardDAO.class);
	// property constants
	public static final String BNAME = "bname";
	public static final String CARD_NO = "cardNo";

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(Bankcard transientInstance) {
		log.debug("saving Bankcard instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void saveOrUpdate(Bankcard transientInstance) {
		log.debug("saving Bankcard instance");
		try {
			getCurrentSession().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Bankcard persistentInstance) {
		log.debug("deleting Bankcard instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Bankcard findById(java.lang.Integer id) {
		log.debug("getting Bankcard instance with id: " + id);
		try {
			Bankcard instance = (Bankcard) getCurrentSession().get(
					"com.huaju.enity.Bankcard", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public Integer countAll() {
		log.debug("finding all Bankcard instances");
		try {
			String queryString = "from Bankcard";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByPage(Integer start, Integer size) {
		log.debug("finding all Bankcard instances");
		try {
			String queryString = "from Bankcard";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setMaxResults(size);
			queryObject.setFirstResult(start);

			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Integer countByProAll(String propertyName, Object value) {
		log.debug("finding all Bankcard instances");
		try {
			String queryString = "from Bankcard as model where model."
					+ propertyName + " like :sname";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter("sname", "%" + value + "%");
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Integer countByProAll(Map<String, String> parameters,String propertyName,String value) {
		log.debug("finding all Bankcard instances");
		try {

			String queryString = "from Bankcard as model where ";
			for (Entry<String, String> entry : parameters.entrySet()) {
				queryString+=" model."+ entry.getKey() + "="+entry.getValue();
			}
			
			queryString += " model."+ propertyName + " like :sname";

			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter("sname", "%" + value + "%");
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByProPage(Integer start, Integer size,Map<String, String> parameters, String propertyName,
			Object value) {
		log.debug("finding all Bankcard instances");
		try {
			String queryString = "select * from bankcard as model ";
			for (Entry<String, String> entry : parameters.entrySet()) {
				queryString+=" model."+ entry.getKey() + "="+entry.getValue();
			}
			
			queryString += " model."+ propertyName + " like :sname";
			SQLQuery queryObject = getCurrentSession().createSQLQuery(
					queryString).addEntity(Bankcard.class);
			queryObject.setParameter("sname", "%" + value + "%");
			queryObject.setMaxResults(size);
			queryObject.setFirstResult(start);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List findByProPage(Integer start, Integer size, String propertyName,
			Object value) {
		log.debug("finding all Bankcard instances");
		try {
			String queryString = "select * from bankcard as model where model."
					+ propertyName + " like :sname";
			SQLQuery queryObject = getCurrentSession().createSQLQuery(
					queryString).addEntity(Bankcard.class);
			queryObject.setParameter("sname", "%" + value + "%");
			queryObject.setMaxResults(size);
			queryObject.setFirstResult(start);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public Integer countAll(String pro,Object value) {
		log.debug("finding all bankcard instances");
		try {
			String queryString = "from Bankcard model where model."+pro+"=?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	
	public List findByPage(Integer start,Integer size,String pro,Object value) {
		log.debug("finding all bankcard instances");
		try {
			String queryString = "from Bankcard model where model."+pro+"=?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			queryObject.setMaxResults(size);
			queryObject.setFirstResult(start);
			
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List<Bankcard> findByExample(Bankcard instance) {
		log.debug("finding Bankcard instance by example");
		try {
			List<Bankcard> results = (List<Bankcard>) getCurrentSession()
					.createCriteria("com.huaju.enity.Bankcard")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Bankcard instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Bankcard as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByAccountAndCardNo(Account user, String cardNo) {
		try {
			String queryString = "from Bankcard as model where model.account= ? and cardNo=?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, user);
			queryObject.setParameter(1, cardNo);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Bankcard> findByBname(Object bname) {
		return findByProperty(BNAME, bname);
	}

	public List<Bankcard> findByCardNo(Object cardNo) {
		return findByProperty(CARD_NO, cardNo);
	}

	public List findAll() {
		log.debug("finding all Bankcard instances");
		try {
			String queryString = "from Bankcard";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Bankcard merge(Bankcard detachedInstance) {
		log.debug("merging Bankcard instance");
		try {
			Bankcard result = (Bankcard) getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Bankcard instance) {
		log.debug("attaching dirty Bankcard instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Bankcard instance) {
		log.debug("attaching clean Bankcard instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static BankcardDAO getFromApplicationContext(ApplicationContext ctx) {
		return (BankcardDAO) ctx.getBean("BankcardDAO");
	}
}